Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true,
    flag: true,
    map: false
  },
  dlxh: function(res) {
    var that = this;
    that.setData({
      dlxh: res.detail.value
    })
  },
  dlsf: function(res) {
    var that = this;
    that.setData({
      dlsf: res.detail.value
    })
  },
  dl: function(res) {
    var that = this;
    wx.request({
      url: 'https://welcome.gongxueyun.com/welcome/yingxin/login.php',
      data: {
        xh: that.data.dlxh,
        sfzh: that.data.dlsf
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        if (res.data.xm !== null) {
          that.setData({
            flag: true,
            xm: res.data.xm,
            xb: res.data.xb,
            ksh: res.data.ksh,
            xh: res.data.xh,
            csrq: res.data.csrq,
            yx: res.data.yx,
            zy: res.data.zy,
            zsdz: res.data.zsdz
          })
          wx.setStorage({
            key: "dl",
            data: res.data
          })
          wx.request({
            url: 'https://welcome.gongxueyun.com/welcome/yingxin/qdtj.php',
            data: {
              xh: that.data.dlxh
            },
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              that.onLoad();
              wx.showToast({
                title: '签到成功',
                icon: 'success',
                duration: 2000
              })
            }
          })
        } else {
          wx.showToast({
            title: '请输入正确信息',
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
  show: function() {
    var that = this;
    var res = wx.getStorageSync('dl')
    console.log(res)
    if (that.data.map == true) {
      if (res) {
        wx.request({
          url: 'https://welcome.gongxueyun.com/welcome/yingxin/qdtj.php',
          data: {
            xh: res.xh
          },
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            that.onLoad();
            wx.showToast({
              title: '签到成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      } else {
        this.setData({
          flag: false
        })
      }
    } else {
      wx.showToast({
        title: '到校了才能签到哦!',
        icon: 'none',
        duration: 2000
      })
    }
  },
  hide: function() {
    this.setData({
      flag: true
    })
  },

  onLoad: function(options) {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude

        function Rad(d) {
          return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
        }

        function map(lat1, lng1, lat2, lng2) {
          var radLat1 = Rad(lat1);
          var radLat2 = Rad(lat2);
          var a = radLat1 - radLat2;
          var b = Rad(lng1) - Rad(lng2);
          var radii = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
          radii = radii * 6378.137;
          // EARTH_RADIUS;
          radii = Math.round(radii * 10000) / 10; //输出米
          return radii
        }
        if (map(latitude, longitude, 30.484419293061123, 114.41617702414634) < 2000) {
          that.setData({
            map: true
          })
        } else {
          that.setData({
            map: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})